package com.xh.one2one.base.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.xh.core.util.OfficeUtils;
import com.xh.one2one.base.bean.dto.SpeechcraftLevelDto;
import com.xh.one2one.base.bean.request.SpeechcraftLevelRequest;
import com.xh.one2one.base.model.ImportSpeechcraft;
import com.xh.one2one.base.model.Speechcraft;
import com.xh.one2one.base.model.SpeechcraftLevel;
import com.xh.one2one.base.dao.SpeechcraftLevelMapper;
import com.xh.one2one.base.service.ISpeechcraftLevelService;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.one2one.base.service.ISpeechcraftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.core.bean.XhPage;
import com.xh.core.util.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 一对一话术等级 服务实现类
 * </p>
 *
 * @author TW
 * @since 2021-05-08
 */
@Service
public class SpeechcraftLevelServiceImpl extends BaseServiceImpl<SpeechcraftLevelMapper, SpeechcraftLevel> implements ISpeechcraftLevelService {

    @Autowired
    private ISpeechcraftService speechcraftService;

    @Override
    public XhPage querySpeechcraftLevelPage(XhPage page, SpeechcraftLevelRequest bean) {
        QueryWrapper qw=new QueryWrapper<SpeechcraftLevel>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List querySpeechcraftLevel(SpeechcraftLevelRequest bean) {
        QueryWrapper qw=new QueryWrapper<SpeechcraftLevel>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return this.dao.selectList(qw);
    }


    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(SpeechcraftLevelRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        return qw;
    }

    /**
     * 获取评语层级树
     * @author 赵文博
     * @date 2021/6/22 14:25
     * @return java.util.List<com.xh.one2one.base.bean.dto.SpeechcraftLevelDto>
     **/
    @Override
    public List<SpeechcraftLevelDto> getSpeechcraftTree() {
        QueryWrapper qw=new QueryWrapper<SpeechcraftLevel>();
        List<SpeechcraftLevelDto> levels = this.selectList(qw,SpeechcraftLevelDto.class);
        List<SpeechcraftLevelDto> levelDtoList = new ArrayList<>();
        for(SpeechcraftLevelDto level : levels){
            if("0".equals(level.getPid())){
                levelDtoList.add(levelTree(levels,level));
            }
        }
        return levelDtoList;
    }

    private SpeechcraftLevelDto levelTree(List<SpeechcraftLevelDto> levels,SpeechcraftLevelDto root){
        for(SpeechcraftLevelDto level : levels){
            if(root.getId().equals(level.getPid())){
                root.getChildes().add(level);
            }
        }
        return root;
    }
    @Override
    public void importExcelToData(String path){
        path = "G:/xiaohe/doc/新舟/微信反馈维度.xlsx" ;

        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        params.setStartSheetIndex(1);
        List<ImportSpeechcraft> list = ExcelImportUtil.importExcel(
                new File(path),
                ImportSpeechcraft.class, params);
        System.out.println(list.size() );

        List<Speechcraft> speechcrafts = new ArrayList<>();
        for (ImportSpeechcraft importSpeechcraft : list) {
            Speechcraft one = new Speechcraft();
            one.setGrade(1);
            one.setText(importSpeechcraft.getOne());
            Speechcraft two = new Speechcraft();
            two.setGrade(3);
            two.setText(importSpeechcraft.getTwo());
            Speechcraft three = new Speechcraft();
            three.setGrade(5);
            three.setText(importSpeechcraft.getThree());

            speechcrafts.add(one);
            speechcrafts.add(two);
            speechcrafts.add(three);
        }

//        speechcraftService.batchInsert(speechcrafts);

    }

}
